/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ficha;

import control.Skill;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author yurifw
 */
public class SkillTableModel extends AbstractTableModel {

    private final int COL_NOME = 0;
    private final int COL_CUSTO = 1;
    private List<Skill> skills;

    public SkillTableModel() {
        skills = new ArrayList<Skill>();
    }

    public SkillTableModel(List<Skill> lista) {
        skills.addAll(lista);
    }

    public int getRowCount() {
        return skills.size();
    }

    public int getColumnCount() {
        return 2;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Skill q = skills.get(rowIndex);

        switch (columnIndex) {
            case COL_NOME:
                return q.getNome();
            case COL_CUSTO:
                return q.getNivel();
            default:
                return "Erro Desconhecido";
        }
    }
    
    @Override
    public void setValueAt (Object valor, int row, int col) {
        Skill q = skills.get(row);
        if (col==COL_CUSTO) {
            skills.get(row).setNivel(Integer.parseInt(valor.toString()));            
        } else if (col==COL_NOME) {
            skills.get(row).setNome(valor.toString());            
        }
    }

    @Override
    public Class getColumnClass (int col) {
        if (col==COL_CUSTO)
            return Integer.class;
        else{
            return String.class;
        }
    }

    @Override
    public boolean isCellEditable (int row, int col) {
        if (col==COL_CUSTO) return false;
        return true;
    }

    public void excluir (int row) {
        skills.remove(row);
        fireTableDataChanged();
    }

    public void excluir (int [] rows) {
        List<Skill> remover = new ArrayList<Skill>();
        for (int i : rows) {
            Skill q = new Skill();
            q=skills.get(i);
            remover.add(q);
        }
        skills.removeAll(remover);
        fireTableDataChanged();
    }

    @Override
    public String getColumnName (int col) {
        if (col==COL_CUSTO)
            return "Pontos";
        if (col==COL_NOME)
            return "Nome";
        return "Erro";
    }

    public void inserir (Skill q) {
        skills.add(q);
        fireTableDataChanged();
    }

    public void limpar () {
        skills.removeAll(skills);
        fireTableDataChanged();
    }

    public void inserir () {
        Skill q = new Skill();
        q.setNome("");
        q.setNivel(0);
        skills.add(q);
        fireTableDataChanged();
    }
    
    public List<Skill> getSkills(){
        return skills;
    }
    
}
